<template>
  <span class="fr">{{timing}}</span>
</template>
<script>
  // 此组件为电话条的计时组件
  import {phonePublishEvent} from '@/store/modules/cti/actions.js'
  export default {
    name: 'PhoneBarTimers',
    watch: {
      mtime (newValue) {
        if (newValue === 'timing') { // 正计时
          this.timingInterval()
        } else if (newValue === 'countDown') { // 倒计时
          this.countDownInterval()
        }
      },
      currentTime (newValue) { // _cti_ui_countTimer一变动的时候就给ctiUiCountTimer重新赋值（每次状态变化时）
        if (newValue) {
          this.ctiUiCountTimer = newValue
        }
      }
    },
    data () {
      return {
        timing: '00:00:00',
        ctiUiCountTimer: 0
      }
    },
    computed: {
      mtime () { // 软电话条计时
        return this.$store.state.cti.globalLet.timingOrCountDown || ''
      },
      currentTime () { // 软电话各个状态需要把时间改变时，重新赋值给局部变量
        let timer = this.$store.state.cti.globalLet._cti_ui_countTimer
        if (!timer) {
          timer = 0
        }
        return timer
      }
    },
    methods: {
      timingInterval () { // 正计时
        let self = this
        if (this.$store.state.cti.globalLet._cti_ui_calculagraph !== null) {
          window.clearInterval(this.$store.state.cti.globalLet._cti_ui_calculagraph)
          let ctiUiCalculagraph = window.setInterval(function () {
            let ctiUiCountTimer = self.ctiUiCountTimer
            self.timing = self.ctiUiGetTimer(ctiUiCountTimer)
            self.ctiUiCountTimer = self.ctiUiCountTimer - 0 + 1
          }, 1000)
          self.$store.commit('CTI_UI_CALCULAGRAPH', ctiUiCalculagraph)
        }
      },
      countDownInterval () { // 后处理，倒计时
        let self = this
        let autoBusyTime = self.$store.state.session.user.AutoBusyTime
        if (autoBusyTime < 1) {
          return
        }
        if (self.$store.state.cti.globalLet._cti_ui_calculagraph !== null) {
          window.clearInterval(self.$store.state.cti.globalLet._cti_ui_calculagraph) // 清除定时器
        }
        let ctiUiCalculagraph = window.setInterval(function () {
          autoBusyTime--
          if (autoBusyTime <= 1) { // 倒计时为0的时候，让正计时也为0
            self.ctiUiCountTimer = 0
            let relogin = window.sessionStorage.getItem('phonebarRelogin')
            if (relogin && relogin === 'yes') { // 解决断网一分钟之后，重连没办法挂断电话的问题
              let status = window.sessionStorage.getItem('phoneBarCurrentStatus')
              if (!status) {
                let loginData = window.sessionStorage.getItem('loginData')
                if (loginData.login_busyType === 'false') { // 忙碌
                  status = '1'
                } else { // 空闲
                  status = '0'
                }
              }
              self.$store.commit('PHONE_DATA', {phoneData: {busyType: status}})
              let ctiObj = self.$store.state.cti
              phonePublishEvent(self.$store.commit, ctiObj, 'toolbarupdate', ctiObj.globalLet._cti_stateDescription[ctiObj.globalLet._cti_currentState], '')
              window.sessionStorage.setItem('phonebarRelogin', 'no')
            }
          }
          self.timing = self.ctiUiAutoBusyTime(autoBusyTime)
        }, 1000)
        this.$store.commit('CTI_UI_CALCULAGRAPH', ctiUiCalculagraph)
      },
      ctiUiGetTimer (countTimer) {
        let minute = '0'
        let second = '0'
        let hour = '0'
        countTimer = parseInt(countTimer) + 1
        hour = parseInt(countTimer / 3600)
        minute = parseInt((countTimer % 3600) / 60)
        second = (countTimer % 3600) % 60
        let mtime = (hour < 10) ? '0' + hour : hour
        mtime += ':'
        mtime += (minute < 10) ? '0' + minute : minute
        mtime += ':'
        mtime += (second < 10) ? '0' + second : second
        return mtime
      },
      ctiUiAutoBusyTime (autoBusyTime) {
        let minute = '0'
        let second = '0'
        let hour = '0'
        if (autoBusyTime >= 60 * 60) {
          hour = parseInt(autoBusyTime / (60 * 60))
          minute = parseInt((autoBusyTime - hour * (60 * 60)) / (60))
          second = autoBusyTime - hour * (60 * 60) - minute * (60)
        } else if (autoBusyTime >= 60 && (autoBusyTime < 60 * 60)) {
          hour = '0'
          minute = parseInt(autoBusyTime / 60)
          second = autoBusyTime - minute * 60
        } else if (autoBusyTime > 0 && autoBusyTime < 60) {
          hour = '0'
          minute = '0'
          second = autoBusyTime
        } else if (autoBusyTime <= 0) {
          hour = '0'
          minute = '0'
          second = '0'
        }
        if (hour < 0) {
          hour = 0
        }
        if (minute < 0) {
          minute = 0
        }
        if (second < 0) {
          second = 0
        }
        let mtime = ((hour < 10) ? ('0' + hour) : hour) + ':' + ((minute < 10) ? ('0' + minute) : minute) + ':' + ((second < 10) ? ('0' + second) : second)
        return mtime
      }
    }
  }
</script>
<style lang="stylus" scoped>

</style>
